package com.web.work;

import com.alibaba.fastjson2.JSON;
import com.web.JDBCUtils;
import com.web.Student;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
//3.通过id查询学生，传入学生id，查询学生信息
@WebServlet("/three")
public class Test3 extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doPost(req,resp);
    }
    @Override
    protected void doPost( HttpServletRequest req,HttpServletResponse resp) throws IOException {
        //设置内容类型
        resp.setContentType("application/json;charset=utf-8");

        String sql = "select * from student where id = ?";
        String id = req.getParameter("id");
        //查询学生信息
        List<Student> students= JDBCUtils.query(Student.class,sql,id);
        //转换数据为JSON格式
        String str = JSON.toJSONString(students);
        resp.getWriter().write(str);
    }
}
